#include <WiFi.h>
#define APSSID "@PHICOMM_0C"
#define APPSK "skabcdef"
const char *ssid = APSSID;
const char *password = APPSK;

#define TARGETIP "192.168.2.182"
#define TARGETPORT 5001
#define LED2 2

// WiFiClient client; //声明一个客户端对象，用于与服务器进行连接
// void wifiClientRequest();
WiFiClient client;
void setup()
{
    Serial.begin(115200);
    Serial.println();
    WiFi.mode(WIFI_STA);  //站点模式
    WiFi.setSleep(false); //关闭STA模式下wifi休眠，提高响应速度
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(2500);
        Serial.print(".");
    }
    Serial.println("connected");
    Serial.print("loca:");
    Serial.println(WiFi.localIP());

    Serial.print("try to server");
    while (!client.connect(TARGETIP, TARGETPORT))
    {
        //如果连接失败则串口输出信息告知用户然后返回loop
        // Serial.println("Sucess : " + String(i));
        Serial.print(".");
    }
    
    Serial.println("\r\nSucess : ");
    String temp = "Hello ESP32! : 1";
    client.println(temp);
    pinMode(LED2, OUTPUT);
}

void loop()
{
    int time = millis() / 1000;
    static int i = 1;
    static bool ledsta = false;
    String str;
    char str1[128];
    String temp = "Data updata :" + String(time / 3600) + String(time / 60 % 60) + String(time % 3600) + "temp:21.0;\talcohol:0.3mg/100ml;\theart rate:67;\tSOP2:99;\tHpress:120\t;Lpress:90\t;";
    client.println(temp);
    delay(50);
    int len = client.available();
    //Serial.println("avab:" + String(len));
    if (len > 0)
    {
        int j;
        for (j = 0; j < len; j++)
        {
            str1[j] = client.read();
        }
        str1[j] = '\0';
        Serial.print("Rec:");
        Serial.println(str1);
    }
    i++;
    Serial.print("send:");
    Serial.println(i);
    digitalWrite(LED2,ledsta);
    ledsta = !ledsta;
    delay(450);
}